#include "offmsgmdl.hpp"
#include "db.hpp"

void offmsgmdl::insert(int id, string message)
{
    // 拼接sql语句
    char sql[1024] = {0};
    sprintf(sql, "insert into offlinemessage values(%d,'%s')", id, message.c_str());
    Mysql mysql;
    if (mysql.connect())
    {
        mysql.update(sql);
    }
}

void offmsgmdl::remove(int id)
{
    // 1.组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "delete from offlinemessage where userid=%d", id);

    Mysql mysql;
    if (mysql.connect())
    {
        mysql.update(sql);
    }
}

vector<string> offmsgmdl::query(int id)
{
    // 1.组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "select message from offlinemessage where userid=%d", id);
    vector<string> rec;
    Mysql mysql;
    if(mysql.connect())
    {
        //查询
        MYSQL_RES* res=mysql.query(sql);
        if(res!=nullptr)
        {
            MYSQL_ROW row;
            while((row=mysql_fetch_row(res))!=nullptr)
            {
                rec.push_back(row[0]);
            }
            mysql_free_result(res);
            return rec;
        }
    }

    return rec;
}

